package com.spirng.leetcode.day04;

public class ReverseString2 {
    public static void main(String[] args) {
        reverseStr("abcdefg",2);//"bacdfeg"
    }
    public static String reverseStr(String s, int k) {
        char[] charArray = s.toCharArray();
        int slow=0;
        int fast=k-1;
        int sum=2*k;
        while (fast<charArray.length&&sum<charArray.length){
            sum+=2*k;
            int n1=slow;
            int n2=fast;
            reverseString(n1,n2,charArray);
            slow=slow+2*k;
            fast=fast+2*k;
        }
        if(charArray.length-(slow)>=k){
            reverseString(slow,slow+k-1,charArray);
        } else if (charArray.length-(slow)<k) {
            reverseString(slow,charArray.length-1,charArray);
        }
        String str="";
        for (int i = 0; i < charArray.length; i++) {
            str+=charArray[i];
        }
        return str;
    }
    public static void reverseString(int n1,int n2,char[] chars){
        while (n1<n2){
            char temp=chars[n1];
            chars[n1]=chars[n2];
            chars[n2]=temp;
            n1++;
            n2--;
        }
    }
}
